import React, {PureComponent} from "react";
import {connect} from "react-redux";
import FormEditor from '../../components/FormEditor';
import {updateCarrierModel} from "./actions";
import {formatDate, formatStrToDate} from "../../lib/func";

class ContractUpdateModal extends PureComponent {

    handleUpdateCarrierModel = contract => {
        const {dispatch, model} = this.props;
        if(contract.expiresDate){
            const dateStr = formatDate(contract.expiresDate);
            const date = formatStrToDate(contract.expiresDate);
            contract.remindDate = date.subtract(30, "day");
            contract.expiresDate = formatStrToDate(dateStr);
        }
        dispatch(updateCarrierModel({...model, "contract": {...contract}}));
    };

    render() {
        const {model} = this.props;
        const schema = [
            {
                title: '基础信息',
                fields: [
                    {
                        field: 'contractName',
                        title: '合同名称',
                        type: 'text',
                        fieldOptions: {
                            rules: [{required: true, message: '请输入合同名称'}]
                        },
                    }, {
                        field: 'contractNumber',
                        title: '合同编号',
                        type: 'text',
                        fieldOptions: {
                            rules: [{required: true, message: '请输入合同编号'}]
                        },
                    }, {
                        field: 'signedDate',
                        title: '签订日期',
                        type: 'date'
                    }, {
                        field: 'expiresDate',
                        title: '结束日期',
                        type: 'date'
                    }, {
                        field: 'remindDate',
                        title: '到期提醒',
                        type: 'date'
                    }
                ]
            }];

        return (

            <FormEditor
                schema={schema}
                column={3}
                defaultReadonly={false}
                showActionBar={false}
                defaultValues={model.contract}
                onChange={this.handleUpdateCarrierModel}
                wrappedComponentRef={inst => (this.formEditor = inst)}
            />
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.carrier.modify
    };
};

export default connect(mapStateToProps)(ContractUpdateModal);
